//
//  UITabBar+HWBadge.m
//  OCDome
//
//  Created by 李含文 on 2020/8/19.
//  Copyright © 2020 李含文. All rights reserved.
//

#import "UITabBar+HWBadge.h"
#import <objc/runtime.h>


// tabbar tag
#define UITabBar_badgeTag(index) (1000 + index)

@interface UITabBar()
/* <#name#> */
@property (nonatomic, strong) UILabel *badgeLabel;

/* <#name#> */
@property (nonatomic, strong) UIImageView *badgeIamgeView;

@end

@implementation UITabBar (HWBadge)

- (void)setBadgeLabel:(UILabel *)badgeLabel {
    objc_setAssociatedObject(self, @selector(badgeLabel), badgeLabel, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UILabel *)badgeLabel {
    UILabel *label = objc_getAssociatedObject(self,_cmd);;
    if (!label) { // 没有值进行关联
        label = [[UILabel alloc] init];
        label.textAlignment = NSTextAlignmentCenter;
        self.badgeLabel = label;
    }
    return label;
}
- (void)setBadgeIamgeView:(UIImageView *)badgeIamgeView {
    objc_setAssociatedObject(self, @selector(badgeIamgeView), badgeIamgeView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIImageView *)badgeIamgeView {
    UIImageView *iamgeView = objc_getAssociatedObject(self,_cmd);;
    if (!iamgeView) { // 没有值进行关联
        iamgeView = [[UIImageView alloc] init];
        iamgeView.backgroundColor = [UIColor clearColor];
        self.badgeIamgeView = iamgeView;
    }
    return iamgeView;
}
- (void)setHw_badgeMinSize:(CGSize)hw_badgeMinSize {
    objc_setAssociatedObject(self, @selector(hw_badgeMinSize),[NSValue valueWithCGSize:hw_badgeMinSize], OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (CGSize)hw_badgeMinSize {
    NSValue *value = objc_getAssociatedObject(self, _cmd);
    if  (value) {
        return [value CGSizeValue];
    }
    return CGSizeMake(12, 12);
}
- (void)setHw_badgeImage:(UIImage *)hw_badgeImage {
    objc_setAssociatedObject(self, @selector(hw_badgeImage), hw_badgeImage, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIImage *)hw_badgeImage {
    return objc_getAssociatedObject(self, _cmd);
}
- (void)setHw_badgeColor:(UIColor *)hw_badgeColor {
    objc_setAssociatedObject(self, @selector(hw_badgeColor), hw_badgeColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIColor *)hw_badgeColor {
    return objc_getAssociatedObject(self, _cmd);
}
- (void)setHw_badgePoint:(CGPoint)hw_badgePoint {
    objc_setAssociatedObject(self, @selector(hw_badgePoint), [NSValue valueWithCGPoint:hw_badgePoint], OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (CGPoint)hw_badgePoint {
    return [objc_getAssociatedObject(self, _cmd) CGPointValue];
}
- (void)setHw_badgeValue:(NSString *)hw_badgeValue {
    objc_setAssociatedObject(self, @selector(hw_badgeValue), hw_badgeValue, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (NSString *)hw_badgeValue {
    return objc_getAssociatedObject(self,_cmd);
}
- (void)setHw_badgeView:(NSString *)hw_badgeView {
    objc_setAssociatedObject(self, @selector(hw_badgeView), hw_badgeView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIView *)hw_badgeView {
    UIView *view = objc_getAssociatedObject(self,_cmd);;
    if (!view) { // 没有值进行关联
        view = [[UIView alloc] init];
        self.hw_badgeView = view;
    }
    return view;
}
- (void)setHw_badgeValueFont:(UIFont *)hw_badgeValueFont {
    objc_setAssociatedObject(self, @selector(hw_badgeValueFont), hw_badgeValueFont, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIFont *)hw_badgeValueFont {
    return objc_getAssociatedObject(self,_cmd);
}
- (void)setHw_badgeValueColor:(UIColor *)hw_badgeValueColor {
    objc_setAssociatedObject(self, @selector(hw_badgeValueColor), hw_badgeValueColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIColor *)hw_badgeValueColor {
    return objc_getAssociatedObject(self,_cmd);
}
// 显示小红点
- (void)hw_showBadgeWithIndex:(NSUInteger )index {
    // 移除
    [self removeRedPointOnIndex:index animation:NO];
    // badgeView(小红点)
    UIView *badgeView = self.hw_badgeView;
    if (self.hw_badgeColor) {
        badgeView.backgroundColor = self.hw_badgeColor;
    } else if (!self.hw_badgeView.backgroundColor) {
        badgeView.backgroundColor = [UIColor redColor];
    }
    if (!badgeView.layer.cornerRadius) {
        badgeView.layer.cornerRadius = self.hw_badgeMinSize.height/2;
    }
    badgeView.tag = UITabBar_badgeTag(index);
    // barButtonView(里面包含文字和图片)
    UIView *barButtonView = [self getBarButttonViewWithIndex:index];
    //（图片的imageView）
    UIView *iconView = nil;
    for (UIView *swappableImageView in barButtonView.subviews) {
        if ([swappableImageView isKindOfClass:[UIImageView class]]) {
            iconView = swappableImageView;
            break;
        }
    }
    CGSize badgeSize = self.hw_badgeMinSize;
    if (self.hw_badgeValue) { // 自适应宽度
        if (self.hw_badgeValueFont) {
            self.badgeLabel.font = self.hw_badgeValueFont;
        } else {
            self.badgeLabel.font = [UIFont systemFontOfSize:9];
        }
        self.badgeLabel.text = self.hw_badgeValue;
        [self.badgeLabel sizeToFit];
        CGFloat w = self.badgeLabel.bounds.size.width;
        if (w+8 > self.hw_badgeMinSize.width) {
            badgeSize = CGSizeMake(w+8, self.hw_badgeMinSize.height);
        }
    }
    // 小红点背景默认值Point设置
    if (CGPointEqualToPoint(self.hw_badgePoint, CGPointZero)) {
        CGSize iconViewSize = iconView.frame.size;
        badgeView.frame = CGRectMake(iconViewSize.width - badgeSize.width / 2, - badgeSize.width / 2, badgeSize.width, badgeSize.height);
    } else {
        badgeView.frame = CGRectMake(self.hw_badgePoint.x, self.hw_badgePoint.y, badgeSize.width, badgeSize.height);
    }
    // 添加图片到小红点上
    if (self.hw_badgeImage) {
        UIImageView *imageview = self.badgeIamgeView;
        imageview.frame = badgeView.bounds;
        imageview.image = self.hw_badgeImage;
        [badgeView addSubview:imageview];
    } else {
        [self.badgeIamgeView removeFromSuperview];
    }
    if (self.hw_badgeValue) {
        UILabel *badgeValue = self.badgeLabel;
        badgeValue.frame = badgeView.bounds;
        if (self.hw_badgeValueColor) {
            badgeValue.textColor = self.hw_badgeValueColor;
        } else {
            badgeValue.textColor = [UIColor whiteColor];
        }
        [badgeView addSubview:badgeValue];
    } else {
        [self.badgeLabel removeFromSuperview];
    }
    //添加小红点到系统图层上
    [iconView addSubview:badgeView];
 }
//隐藏小红点
- (void)hw_hiddenBadgeWithIndex:(NSUInteger )index animation:(BOOL)animation {
    [self removeRedPointOnIndex:index animation:animation];
}

- (void)removeRedPointOnIndex:(NSUInteger )index animation:(BOOL )animation {
    // 获取对应的barButtonView（里面包含文字和图片）
    UIView *barButtonView = [self getBarButttonViewWithIndex:index];
    for (UIView *swapView in barButtonView.subviews) {
        if ([swapView isKindOfClass:[UIImageView class]]) {       //遍历出图片类的uiview图层_iconView
            for (UIView *view in swapView.subviews) {
                if (view.tag == UITabBar_badgeTag(index)) {                //找到了小红点
                    if (animation) {
                        [UIView animateWithDuration:0.2 animations:^{
                            view.transform = CGAffineTransformScale(view.transform, 2, 2);
                            view.alpha = 0;
                        } completion:^(BOOL finished) {
                            [view removeFromSuperview];
                        }];
                    } else  {
                        [view removeFromSuperview];
                    }
                }
            }
        }
    }
}


// 获取barButtonView
- (UIView *)getBarButttonViewWithIndex:(NSUInteger )index {
    UIBarButtonItem *item = (UIBarButtonItem *)[self.items objectAtIndex:index];
    UIView *barButtonView = [item valueForKey:@"view"];
    return barButtonView;
}
@end
